package com.project.service;

import com.project.dao.MagFacultyInfoMapper;
import com.project.entity.MagFacultyInfo;
import com.project.entity.User;
import com.project.myBeans.MagfacultyInfoDetail;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by hasee on 2017/3/21.
 */

@Transactional(rollbackFor=Exception.class)
@Service
public class MagFacultyInfoServiceImpl extends BaseService implements MagFacultyInfoService {
    @Autowired
    private UserService userService;

    private static MagFacultyInfoMapper magFacultyInfoMapper = null;
    public void setMapper()
    {
        if(magFacultyInfoMapper==null)
            magFacultyInfoMapper = sqlSessionTemplate.getMapper(MagFacultyInfoMapper.class);//1.获取相关mapper
    }

    /**
     * author: WheelChen
     * date: 2017/6/23
     * function: 根据userId查找行政人员信息
     *
     * @param userId
     * @return
     */
    @Override
    public MagFacultyInfo findByUserId(Integer userId) {
        setMapper();
        return magFacultyInfoMapper.selectByUserId(userId);
    }

    /**
     * author: WheelChen
     * date: 2017/6/23
     * function:新增行政人员信息
     *
     * @param magFacultyInfo
     * @return
     */
    @Override
    public int add(MagFacultyInfo magFacultyInfo) {
        setMapper();
        return magFacultyInfoMapper.insertSelective(magFacultyInfo);
    }

    /**
     * author: WheelChen
     * date: 2017/6/23
     * function: 根据userId修改行政人员信息
     *
     * @param magFacultyInfo
     * @return
     */
    @Override
    public int modifyByUserId(MagFacultyInfo magFacultyInfo) {
        setMapper();
        return magFacultyInfoMapper.updateByUserId(magFacultyInfo);
    }

    /**
     * author: WheelChen
     * date: 2017/6/24
     * function:根据id修改行政人员信息
     *
     * @param magFacultyInfo
     * @return
     */
    public int modifyById(MagFacultyInfo magFacultyInfo){
        setMapper();
        return magFacultyInfoMapper.updateByPrimaryKey(magFacultyInfo);
    }

    /** Todo 添加学院管理员
     * author: WheelChen
     * date: 2017/7/13
     * function: 添加学院管理员账号
     *
     * @param user           用户
     * @param magFacultyInfo 行政
     * @return
     */
    @Override
    public int addManageAccount(User user, MagFacultyInfo magFacultyInfo) throws Exception {
        int res1 = userService.createUser(user);
        int userId = user.getId();
        magFacultyInfo.setUserid(userId);
        return add(magFacultyInfo);
    }

    /**
     * author: WheelChen
     * date: 2017/8/9
     * function:
     *
     * @param userId
     * @return
     */
    @Override
    public int removeOneByUserId(Integer userId) {
        setMapper();
        return magFacultyInfoMapper.deleteOneByUserId(userId);
    }

    /**
     * author: WheelChen
     * date: 2017/8/9
     * function: 根据用户表、行政人员信息表条件筛选
     *
     * @param magfacultyInfoDetail
     * @return
     */
    @Override
    public List<MagfacultyInfoDetail> findBatchByCondition(MagfacultyInfoDetail magfacultyInfoDetail) {
        setMapper();
        return magFacultyInfoMapper.selectBatchByCondition(magfacultyInfoDetail);
    }
}
